package com.wang.study.commons;

import org.apache.ibatis.annotations.Param;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.List;
import java.util.Properties;
import java.util.Random;

public class CommonsUtils {
    //获取激活码
    public static String getRandomString(int length){
        String source ="abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ123456789";
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            builder.append(source.charAt(new Random().nextInt(source.length())));
        }
        return builder.toString();
    }
    //发送邮件
    public static void getSendMail(String sender,String recipient,String code,String title,String context){
        try {
            //创建properties:用于设置发送邮件的基本环境
            Properties properties = new Properties();
            //协议
            properties.setProperty("mail.transport.protocol", "SMTP");
            //邮箱厂商
            properties.setProperty("mail.host", "smtp.163.com");
            //授权
            properties.setProperty("mail.smtp.auth", "true");

            //创建Session
            Session session = Session.getInstance(properties, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(sender, code);
                }
            });

            //1、创建邮件
            Message message = new MimeMessage(session);
            //设置邮件的发送者
            message.setFrom(new InternetAddress(sender));
            //设置邮件的接收者
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
            //设置邮件标题
            message.setSubject(title);
            //设置邮件内容
            //参数1：邮件内容
            //参数2：邮件的内容类型
            message.setContent(context, "text/html;charset=utf-8");

            //发送邮件
            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
    //加密
    public static String getMD5(String str) {
        String s = null;
        try {
            String password = str;
            //生成加密对象
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            //调用digest()方法对字符串进行加密操作
            byte[] bytes = md5.digest(password.getBytes("utf-8"));
            StringBuilder ss = new StringBuilder();
            for (byte b : bytes) {
                ss.append(b);
            }
            //使用Base64对加密结果进行处理，生成对应的字符串
            s = Base64.getEncoder().encodeToString(bytes);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return s;
    }
    public static String getDelUserSql(@Param("ids") List<Integer> ids){
        String sql="delete from mall_cart where id in(";
        for (int i = 0; i < ids.size(); i++) {
            if(i<ids.size()-1) {
                sql += ids.get(i) + ",";
            }else {
                sql += ids.get(i) + ")";
            }
        }
        return sql;
    }
}
